package players;

import staticClasses.*;

public abstract class Player extends Agent{
	
	public boolean isInLocalTeam=true;
	public String name;
	public Action nextAction = Action.STAND;
	
	public int refreshAction=0;
	
	/**
	 * which behavior to choose between EASY, MEDIUM, HARD.
	 * it reflect how "smart" is the player
	 */
	public Behavior behavior;
	
	/**
	 * State of the ball
	 * 0 = normal
	 * 1 = the player has just passed the ball to a teammate
	 * 2 = the player has been targeted by a teammate to receive the ball
	 */
	public int ballState=0;
	
	/**
	 * This value represent how well a player is situated in the field
	 * if nobody is near him and if he is near the opponent's goal,the value will be very high.
	 * if he is defending with 3 opponents closer than 2 meters, the value will be near 0
	 * 
	 * This value isn't used in the "EASY" behavior, but it permit to do good passes in higher levels.
	 * This value refresh every "tick"
	 */
	public int wellPlaced=0;
	
	/**
	 * if i'm on the ball or running to the ball this boolean is set to true.
	 * It allow the team to know that somebody is going to the ball and not run all to the ball
	 */
	public boolean imOnTheBall = false;
	
	/**
	 * In the MEDIUM difficulty, we need to know if a player is marked or not.
	 * If he is marked, we save here the player who marked him.
	 */
	private Player isMarked = null;
	
	/**
	 * Initial square where the player is supposed to be
	 */
	public Square initialSpot;
	
	/**
	 * Global level of the player.
	 * It decides the values below and the behavior to choose.
	 * It reflect how good is a player by his statistics, but also
	 * how intelligent he is.
	 */
	public PlayerLevel globalLevel;

	/**
	 * how good is the player.
	 * values from 0 to 255
	 */
	public short dribble_level;
	public short speed;
	public short power;
	public short intercept_level;
	public short accuracy;
	public short stamina;
	
	public Player(Square init){
		this.initialSpot = init;
		this.setPosition(init.x, init.y);
	}
	
	public synchronized void setIsMarked(Player p){
		this.isMarked = p;
	}
	
	public synchronized Player getIsMarked(){
		return this.isMarked;
	}
	
	/**
	 * 
	 */
	@Override
	public void run(){
		while(Global.play){
			try {
				synchronized (this) {
					this.wait();
				}
				
				if(this.refreshAction>=19){
					if(this.globalLevel==PlayerLevel.MEDIUM){
						System.out.println(this+" - "+this.nextAction);
					}
					this.refreshAction=0;
					this.nextAction = this.behavior.whatToDo();
				}
				this.behavior.execAction();
				this.refreshAction++;
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	public abstract void setLevel(PlayerLevel level);
}
